// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata mkt_d_ind_sum.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mkt_d_ind_sum_log, replace text

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mkt_d_ind_sum/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "est msa1983 zipcode fips"
global vmkt_sel = "msa1983"

tempfile ds_temp
global ds_temp = "`ds_temp'"

/*
================================================================================
Plot at industry level
Y: d ln(selected variables)
X: d ln(markets per firm)
*/

capture program drop fig_sum
program fig_sum

	args y y_lab fig_rt
    
foreach vmkt in $lvmkt {
	
	//local vmkt = "msa"
	//local y = "ln_emp_aux" 
	local x = "ln_mu_mkt_`vmkt'_d"
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
	
	*Generate 4sf rounded variables for plotting (to be disclosure-compliant)
	capture gen `x'_4sf = `x'
	capture gen `y'_4sf = `y'
	rounding_4dig "`y'_4sf `x'_4sf"
	sum `x'
	sum `x'_4sf
	sum `y'
	sum `y'_4sf
	
	//egen maxx = pctile(`x'), p(95)
	//egen minx = pctile(`x'), p(5)
	
/* Redacted */
        
	//drop maxx
	//drop minx
	
	/*
	twoway (scatter `y' `x', msize(vsmall)) ///
		(lpolyci `y' `x' [aw=w_sv], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI")
	*/
	twoway ///
		(lpolyci `y'_4sf `x'_4sf [aw=w_sv], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI")
	
	//graph export ${dir_outf}/def`idef'/Y_l_emp_aux_d_X_l_mkt_d_`vmkt'.png, replace width(3000) height(2000)
	graph export ${dir_outf}/`fig_rt'_`vmkt'.png, replace width(3000) height(2000)
    
    reg `y' `x' [aw=w_sv]
    f_reg_append "fr_reg" "`x'"

}
	
    //local y = "ln_emp_aux" 
    local x1 = "ln_mu_mkt_est_d"
    local x2 = "ln_mu_mkt_fips_d"
    local x3 = "ln_mu_mkt_msa1983_d"
    local x4 = "ln_mu_mkt_zip_d"
    
	capture gen `x1'_4sf = `x1'
	capture gen `x2'_4sf = `x2'
	capture gen `x3'_4sf = `x3'
	capture gen `x4'_4sf = `x4'
	rounding_4dig "`x1'_4sf `x2'_4sf `x3'_4sf `x4'_4sf"

   
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of Markets of All Firms)"'
	
	/*
	twoway (lpoly `y' `x1' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x2' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x3' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x4' [aw=w_emps], lw(med) fi(inten0)) ///
        (lpoly `y' `x5' [aw=w_emps], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "FIPS" 3 "MSA" 4 "ZIP" 5 "CZONE") rows(1)) ///
		note("99% CI")
     
    graph export ${dir_outf}/z_`fig_rt'_all.png, replace width(3000) height(2000)
	*/
    
    twoway (lpoly `y'_4sf `x1'_4sf [aw=w_sv], lw(med) fi(inten0)) ///
        (lpoly `y'_4sf ln_mu_mkt_${vmkt_sel}_d_4sf [aw=w_sv], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "${vmkt_sel}") rows(1)) ///
		note("99% CI")
     
    graph export ${dir_outf}/z_`fig_rt'_sel.png, replace width(3000) height(2000)
	
end

/*
================================================================================
*/

f_reg_create "fr_reg" "1"

/*
--------------------------------------------------------------------------------
Y does not involve sales
*/

do ${dir_do}/mkt_d_ind_0.do "geo"
save ${ds_temp}_a, replace


fig_sum "ln_emp_ind_d" `"{&Delta} ln(Ind Emp)"' "Y_ln_emp_ind_d_X_ln_mkt_d"
fig_sum "ln_pay_ind_d" `"{&Delta} ln(Ind Pay)"' "Y_ln_pay_ind_d_X_ln_mkt_d"
/*
// Employment or sales of bottom 90% firms
fig_sum "ln_emp_ind_d_10b" `"{&Delta} ln(Bottom 90% Emp)"' "Y_ln_emp_ind_d_10b_X_ln_mkt_d"

// Average Market Size
fig_sum "ln_mktsize_zip_d" `"{&Delta} ln(Average ZIP Size)"' "Y_ln_mktsize_zip_d_X_ln_mkt_d"
fig_sum "ln_mktsize_fips_d" `"{&Delta} ln(Average FIPS Size)"' "Y_ln_mktsize_fips_d_X_ln_mkt_d"
fig_sum "ln_mktsize_msa1983_d" `"{&Delta} ln(Average MSA1983 Size)"' "Y_ln_mktsize_msa1983_d_X_ln_mkt_d"
fig_sum "ln_mktsize_msa1983cz_d" `"{&Delta} ln(Average MSA1983CZ Size)"' "Y_ln_mktsize_msa1983cz_d_X_ln_mkt_d"
*/

/*For disclosure: can remove later */
keep *4sf w_sv
save "/projects/disclosure/Apr2021/support/plots_data/section2/emp_pay_plots.dta", replace

/*
--------------------------------------------------------------------------------
Y involves sales
Note that the X axis is slightly different (because we choose the starting year at the industry level)

*/
clear
do ${dir_do}/mkt_d_ind_0.do "geo" "" "sales"
save ${ds_temp}_a, replace
fig_sum "ln_sales_ind_d" `"{&Delta} ln(Ind Sales)"' "Y_ln_sales_ind_d_X_ln_mkt_d"
fig_sum "ln_sales_ind_d_10b" `"{&Delta} ln(Bottom 90% Sales)"' "Y_ln_sales_ind_d_10b_X_ln_mkt_d"



f_reg_save "fr_reg" "${dir_outf}/reg_mkt_d_ind_sum"



di "Ended at $S_DATE $S_TIME"
capture log close
// End of do file
